﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Utils;

namespace QldtSdh.GUI.DuLieu
{
    public partial class FormQuanLyKhoaHoc : QldtSdh.GUI.Common.BaseDialog
    {
        bool allowUpdate = false;
        public FormQuanLyKhoaHoc()
        {
            InitializeComponent();
            gridDataKhoaHoc.AddRowIndexing();
            gridDataNganhKhoaHoc.AddRowIndexing();
            InitGridKhoaHoc();
            InitGridNganhKhoaHoc();
            LoadKhoaHoc();
        }

        void InitGridKhoaHoc()
        {
            gridDataKhoaHoc.AutoGenerateColumns = false;
            colTenKhoaHoc.DataPropertyName = "TenKhoaHoc";
            colNamTuyenSinh.DataPropertyName = "NamTuyenSinh";
            colDangHoatDong.DataPropertyName = "DangHoatDong";
        }

        void InitGridNganhKhoaHoc()
        {
            gridDataNganhKhoaHoc.AutoGenerateColumns = false;
            colMaNganh.DataPropertyName = "MaNganhDaoTao";
            colTenNganh.DataPropertyName = "TenNganhDaoTao";
            colChuongTrinhDaoTao.DataPropertyName = "TenChuongTrinhDaoTao";
        }

        void LoadKhoaHoc()
        {
            allowUpdate = false;
            var ls = BizKhoaHoc.GetList();
            bdKhoaHoc.DataSource = ls;
            gridDataKhoaHoc.DataSource = bdKhoaHoc;
            if (ls != null && ls.Count > 0)
            {
                btnSuaKhoaHoc.Enabled = btnXoaKhoaHoc.Enabled = true;
                btnThemNganh.Enabled = true;
            }
            else
            {
                btnSuaKhoaHoc.Enabled = btnXoaKhoaHoc.Enabled = false;
                btnThemNganh.Enabled = false;
            }
            LoadNganhKhoaHoc();
        }

        void LoadNganhKhoaHoc()
        {
            allowUpdate = false;
            var current = bdKhoaHoc.Current as KhoaHoc;
            if (current != null)
            {
                var ls = BizKhoaHoc_NganhDaoTao.GetListByKhoaHoc(current.MaKhoaHocGUID);
                bdNganhKhoaHoc.DataSource = ls;
                gridDataNganhKhoaHoc.DataSource = bdNganhKhoaHoc;
                if (ls != null && ls.Count > 0)
                {
                    btnSuaNganh.Enabled = btnXoaNganh.Enabled = true;
                }
                else
                {
                    btnSuaNganh.Enabled = btnXoaNganh.Enabled = false;
                }
            }
            allowUpdate = true;
        }

        private void bdKhoaHoc_CurrentChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
                var current = bdKhoaHoc.Current as KhoaHoc;
                if (current != null)
                    LoadNganhKhoaHoc();
            }
        }

        private void btnThemKhoaHoc_Click(object sender, EventArgs e)
        {
            var fdg = new FormQuanLyKhoaHoc_ChiTiet();
            fdg.OnChanged += (o, t) =>
            {
                LoadKhoaHoc();
            };
            fdg.ShowDialog();
        }

        private void btnThemNganh_Click(object sender, EventArgs e)
        {
            var current = bdKhoaHoc.Current as KhoaHoc;
            if (current != null)
            {
                var fdg = new FormQuanLyKhoaHoc_BoSungNganhDaoTao(current);
                fdg.OnChanged += (o, t) =>
                {
                    LoadNganhKhoaHoc();
                };
                fdg.ShowDialog();
            }
        }

        private void btnSuaKhoaHoc_Click(object sender, EventArgs e)
        {
            var current = bdKhoaHoc.Current as KhoaHoc;
            if (current != null)
            {
                var fdg = new FormQuanLyKhoaHoc_ChiTiet(current);
                if (fdg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    LoadKhoaHoc();
            }
        }

        private void btnXoaKhoaHoc_Click(object sender, EventArgs e)
        {
            var current = bdKhoaHoc.Current as KhoaHoc;
            if (current != null)
            {
                if (DialogUtils.ShowOKCancel(NOTIFY_CONSTANT_STRING.DELETE) == System.Windows.Forms.DialogResult.OK)
                {
                    var rs = BizKhoaHoc.Delete(current);
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                        bdKhoaHoc.RemoveCurrent();
                    else
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_XOA, ENTITY_NAME.KHOAHOC));
                }
            }
        }

        private void btnSuaNganh_Click(object sender, EventArgs e)
        {
            var khoaHoc = bdKhoaHoc.Current as KhoaHoc;
            if (khoaHoc != null)
            {
                var khoaHoc_Nganh = bdNganhKhoaHoc.Current as KhoaHoc_NganhDaoTao;
                if (khoaHoc_Nganh != null)
                {
                    var fdg = new FormQuanLyKhoaHoc_BoSungNganhDaoTao(khoaHoc, khoaHoc_Nganh);
                    if (fdg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        LoadNganhKhoaHoc();
                }
            }
        }

        private void btnXoaNganh_Click(object sender, EventArgs e)
        {
            var current = bdNganhKhoaHoc.Current as KhoaHoc_NganhDaoTao;
            if (current != null)
            {
                if (DialogUtils.ShowOKCancel(NOTIFY_CONSTANT_STRING.DELETE) == System.Windows.Forms.DialogResult.OK)
                {
                    var rs = BizKhoaHoc_NganhDaoTao.Delete(current);
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                        bdNganhKhoaHoc.RemoveCurrent();
                    else
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_XOA, ENTITY_NAME.CHUYENGANHDAOTAO));
                }
            }
        }

        private void bdNganhKhoaHoc_CurrentChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
                var current = bdNganhKhoaHoc.Current as KhoaHoc_NganhDaoTao;
                if (current != null)
                {
                    btnSuaNganh.Enabled = btnXoaNganh.Enabled = true;
                }
                else
                    btnSuaNganh.Enabled = btnXoaNganh.Enabled = false;
            }
        }
    }
}
